package com.cm.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cm.common.core.Result;
import com.cm.common.utils.COSStorage;
import com.cm.common.utils.StringUtil;
import com.cm.goods.entity.Brand;
import com.cm.goods.entity.dto.BrandKeyWordDTO;
import com.cm.goods.entity.vo.BrandVO;
import com.cm.goods.mapper.BrandMapper;
import com.cm.goods.service.BrandService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @类 名:BrandController </br>
 * @描 述:<br>
 * @日 期:2020/9/23 17:20 </br>
 * @作 者:小兰同学</br>
 * @版 本：1.0.0
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    BrandService brandService;

    @PostMapping("/insert")
    public Result insert (@RequestBody Brand brand){
        System.out.println(brand);
        if (brandService.insert(brand)) {
            return Result.success("品牌添加成功");
        }
        return Result.fail(brand.getName() + "已存在！");
    }

    @PostMapping("/delete")
    public Result delete(Long id) {
        if (brandService.delete(id)) {
            return Result.success("删除成功");
        }
        return Result.fail("删除失败");
    }

    @PostMapping("/update")
    public Result update(@RequestBody Brand brand) {
        if (brandService.update(brand)) {
            return Result.success("修改成功");
        }
        return Result.fail("修改失败");
    }

    @PostMapping("/list")
    public Result getBrand(@RequestBody BrandKeyWordDTO brandKeyWordDTO) {
        IPage<BrandVO> userIPage = brandService.getBrandList(brandKeyWordDTO);
        return Result.success(userIPage);
    }

    @PostMapping("/getById")
    public Result getById(Long id){
        Brand brand=brandMapper.selectById(id);
        return Result.success(brand);
    }
    @GetMapping("/select")
    public Result select(){
        QueryWrapper queryWrapper=new QueryWrapper();
        List<Brand> brandList = brandMapper.selectList(queryWrapper);
        return Result.success(brandList);
    }
    @PostMapping("/setLogo")
    public Result upload(MultipartFile file) throws IOException {
        String filename=file.getOriginalFilename();
        InputStream inputStream=file.getInputStream();
        COSStorage cosStorage = new COSStorage();
        String path = cosStorage.upload(inputStream, StringUtil.getFileExt(filename));
        return Result.success(path);
    }
}
